//
//  CurrencyDetailsView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/2.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class CurrencyDetailsView: BaseView, UITableViewDelegate, UITableViewDataSource {
    var viewModel:CurrencyDetailsViewModel?
    lazy var headView: UIView = {
       let view = UIView()
       view.backgroundColor = kTitleColor
        return view
    }()
    
    lazy var buyCoinLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexStr:"#3cb265")
        label.font = UIFont.systemFont(ofSize: 20)
        return label
    }()
    
    lazy var positionBtn: UIButton = {
        let btn = UIButton()
        btn.isHidden = true
        btn.layer.cornerRadius = 15
        btn.isHidden = true
        btn.layer.masksToBounds = true
        btn.setTitle("平仓", for: UIControlState.normal)
        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        btn.backgroundColor = kThemeColor
        return btn
    }()
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.themeBackground = ThemeManager.ThemeColorKeys.background
        return tableView
    }()
    
    
    

    override func setUpUI() -> Void {
        viewModel = dataSource as? CurrencyDetailsViewModel
        
        let position = viewModel?.model.position == 0 ? kText("买入") : kText("卖出")
        let coinName = String(format: "%@ %@/%@",position,(viewModel?.model.targetCoin)!,(viewModel?.model.sourceCoin)!)
        buyCoinLabel.text = coinName
        buyCoinLabel.textColor = position == kText("买入") ? kGreenColor :  kRedColor
        
        self.addSubview(headView)
        headView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(47)
        }
      
       headView.addSubview(buyCoinLabel)
        buyCoinLabel.changeLabelTextStyle(title: String(format: "%@/%@",(viewModel?.model.targetCoin)!,(viewModel?.model.sourceCoin)!), color: UIColor.white, font: buyCoinLabel.font)
        buyCoinLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15)
        }
        
        
        self.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { (make) in
            make.top.equalTo(headView.snp.bottom)
            make.bottom.left.right.equalToSuperview()
        }
        
//        self.addSubview(positionBtn)
//        positionBtn.snp.makeConstraints { (make) in
//            make.right.equalToSuperview().offset(-15)
//            make.centerY.equalTo(buyCoinLabel)
//            make.width.equalTo(80)
//            make.height.equalTo(30)
//        }
        
    }
    
    //MARK: tableViewDelegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (viewModel?.titleArr[section].count)!
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return (viewModel?.titleArr.count)!
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellName = "identy"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellName) as? BaseTableViewCell
        if cell == nil {
            cell = BaseTableViewCell(style: .default, reuseIdentifier: cellName)
            cell?.selectionStyle = .none
            cell?.nameLabel.font = UIFont.systemFont(ofSize: 13)
            cell?.line.removeFromSuperview()
            cell?.kindLabel.font = UIFont.systemFont(ofSize: 11)
            cell?.kindLabel.themeTextColor = ThemeManager.ThemeColorKeys.text1
            cell?.nameLabel.themeTextColor = ThemeManager.ThemeColorKeys.text2
            cell?.contentView.addSubview((cell?.kindLabel)!)
            cell?.kindLabel.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.right.equalToSuperview().offset(-16)
            }
            if viewModel?.type == .leverPositionType ||  viewModel?.type == .leverHistory {
                if indexPath.section == 0 && indexPath.row == 1 {
                    cell?.kindLabel.snp.updateConstraints({ (make) in
                        make.right.equalToSuperview().offset(-85)
                    })
                    
                    let statesLabel = UILabel()
                    statesLabel.text = viewModel?.statesStr
                    statesLabel.layer.cornerRadius = 12.5
                    statesLabel.layer.masksToBounds = true
                    statesLabel.textColor = UIColor.white
                    statesLabel.textAlignment = NSTextAlignment.center
                    statesLabel.font = UIFont.systemFont(ofSize: 10)
                    statesLabel.backgroundColor = viewModel?.model.position == 0 ? kGreenColor : kRedColor
                    cell?.contentView.addSubview(statesLabel)
                    statesLabel.snp.makeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.right.equalToSuperview().offset(-15)
                        make.width.equalTo(60)
                        make.height.equalTo(25)
                    }
                }
            }
        }
        if viewModel?.type == .leverPositionType ||  viewModel?.type == .leverHistory {
            if indexPath.section == 0 {
                if indexPath.row == 0 {
                    
                }else if indexPath.row == 1 {
                    cell?.kindLabel.text = String(format: "%@/%@", (viewModel?.model.targetCoin)!,(viewModel?.model.sourceCoin)!)
                }else {
                    
                }
                
            }else {
                
//                cell?.kindLabel.text = viewModel?.dataArr[indexPath.section][indexPath.row]
            }
        }else {
           cell?.kindLabel.text = viewModel?.dataArr[indexPath.section][indexPath.row]
        }
        
        cell?.nameLabel.text = viewModel?.titleArr[indexPath.section][indexPath.row]
        
        return cell!
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 33
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 8))
        return view
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 8))
//        view.backgroundColor = UIColor.blue
        if section == 0 || ((viewModel?.titleArr.count)!>2 && section == 1){
            let imageView = UIImageView(frame: CGRect(x: 0, y: 5, width: kSCREEN_WIDTH, height: 3))
            view.addSubview(imageView)
            let image =  self.drawLineOfDashByImageView(imageView: imageView)
            imageView.image = image
        }
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 8
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 8
    }
    
    func drawLineOfDashByImageView(imageView:UIImageView) -> UIImage {
        UIGraphicsBeginImageContext(imageView.frame.size)
        imageView.image?.draw(in: CGRect(x: 0, y: 0, width: imageView.frame.size.width, height: imageView.frame.size.height))
        let line = UIGraphicsGetCurrentContext()
        line!.setLineCap(CGLineCap.round)
        line!.setStrokeColor(UIColor.gray.cgColor)
        line!.setLineDash(phase: 2, lengths: [2,2])
        line?.move(to: CGPoint(x: 15.0, y: 2.0))
        line?.addLine(to: CGPoint(x: kSCREEN_WIDTH-15, y: 2.0))
        line?.strokePath()
        return UIGraphicsGetImageFromCurrentImageContext()!
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
